import type { Article, User, Tags, Comment } from "@/types";
import type { CarouselSlide } from "@/types";
import {
  getMockUsers,
  getMockComments,
  getMockAllTags,
  addMockComment,
  updateCommentLikes,
  updateMockComments,
} from "@/Utils/localForage";

// 模拟轮播图数据
export const mockCarouselSlides:CarouselSlide[]=[
  {
    id: "1",
    title: "React 18 新特性详解",
    imageUrl: "https://picsum.photos/400/200?random=1",
    link: "https://www.baidu.com",
    description: "React 18 新特性详解",
    },
    {
    id: "2",
    title: "如何实现页面丝滑跳转 startViewTransition",
    imageUrl: "https://picsum.photos/400/200?random=2",
    link: "https://developer.mozilla.org/zh-CN/docs/Web/API/Document/startViewTransition",
    description: "一个全新的 API，用于实现页面丝滑跳转",
    },
    {
    id: "3",
    title: "实现动画效果的一种方式 ——Framer motion",
    imageUrl: "/public/images/FramerMotion.jpg",
    link: "https://www.baidu.com",
    description: "Framer motion 是一个用于实现动画效果的库，它可以帮助我们实现各种动画效果",
    },
    {
    id: "4",
    title: "通过 IntersectionObserver 实现目录锚点跟踪",
    imageUrl: "https://picsum.photos/400/200?random=4",
    link: "https://ruanyifeng.com/blog/2016/11/intersectionobserver_api.html",
    description: "IntersectionObserver 是一个用于实现目录锚点跟踪的 API，它可以帮助我们实现目录锚点跟踪, 但是这个API在低版本浏览器中不支持，所以需要使用polyfill来实现",
    },
]
// 模拟用户数据
export const mockUsers: User[] = [
  {
    id: "1",
    username: "张三",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=1",
    bio: "前端开发工程师",
    followers: 120,
    following: 45,
    articles: 15,
  },
  {
    id: "2",
    username: "李四",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=2",
    bio: "全栈开发工程师",
    followers: 200,
    following: 80,
    articles: 25,
  },
  {
    id: "3",
    username: "王五",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=3",
    bio: "后端开发工程师",
    followers: 150,
    following: 60,
    articles: 18,
  },
  {
    id: "4",
    username: "赵六",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=4",
    bio: "UI/UX 设计师",
    followers: 180,
    following: 70,
    articles: 12,
  },
  {
    id: "5",
    username: "钱七",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=5",
    bio: "DevOps 工程师",
    followers: 90,
    following: 35,
    articles: 8,
  },
  {
    id: "6",
    username: "孙八",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=6",
    bio: "移动端开发工程师",
    followers: 110,
    following: 50,
    articles: 14,
  },
  {
    id: "7",
    username: "周九",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=7",
    bio: "数据科学家",
    followers: 160,
    following: 65,
    articles: 20,
  },
  {
    id: "8",
    username: "吴十",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=8",
    bio: "产品经理",
    followers: 130,
    following: 55,
    articles: 6,
  },
  {
    id: "9",
    username: "郑十一",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=9",
    bio: "测试工程师",
    followers: 85,
    following: 40,
    articles: 10,
  },
  {
    id: "10",
    username: "王十二",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=10",
    bio: "架构师",
    followers: 220,
    following: 90,
    articles: 30,
  },
  {
    id: "11",
    username: "刘十三",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=11",
    bio: "前端架构师",
    followers: 190,
    following: 75,
    articles: 22,
  },
  {
    id: "12",
    username: "陈十四",
    avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=12",
    bio: "技术博主",
    followers: 300,
    following: 120,
    articles: 45,
  }
];
// 模拟文章数据
export const mockArticles: Article[] = [
  {
    id: "1",
    title: "React 18 新特性详解",
    content: `
# React 18 新特性详解

## 1. 并发渲染
React 18 引入了并发渲染的概念，这是一个重要的架构更新。

### 1.1 并发特性
- 自动批处理
- 并发更新
- 并发模式

### 1.2 使用场景
- 数据获取
- 状态更新
- 动画效果

## 2. Suspense 改进
Suspense 组件在 React 18 中得到了显著增强。

### 2.1 服务端渲染
- 流式渲染
- 选择性水合
- 并发特性支持

### 2.2 数据获取
- 内置数据获取
- 错误边界集成
- 加载状态处理

## 3. 新的 Hooks
React 18 引入了一些新的 Hooks。

### 3.1 useTransition
用于标记非紧急更新。

### 3.2 useDeferredValue
用于延迟更新某些值。

## 4. 性能优化
React 18 带来了多项性能优化。

### 4.1 渲染优化
- 更智能的调度
- 更好的内存使用
- 更快的首次加载

### 4.2 开发体验
- 更好的开发工具
- 更详细的警告信息
- 更好的调试体验
    `,
    author: mockUsers[0],
    cover: "https://picsum.photos/400/200?random=1",
    summary: "React 18 新特性详解",
    publishTime: "2025-06-15T11:00:00Z",
    createTime: "2025-06-10T16:00:00Z",
    updateTime: "2024-06-16T11:00:00Z",
    tags: ["React", "前端", "JavaScript"],
    likes: 120,
    comments: 15,
    views: 1000,
    stars: 10,
    category:'other',
    rank:0,
    difficulty:'easy',
    isHot:true
  },
  {
    id: "2",
    title: "TypeScript 高级类型实战",
    content: `
# TypeScript 高级类型实战

## 1. 类型系统
TypeScript 的类型系统非常强大。

### 1.1 基础类型
- 基本类型
- 联合类型
- 交叉类型

### 1.2 高级类型
- 映射类型
- 条件类型
- 索引类型

## 2. 泛型
泛型是 TypeScript 中最重要的特性之一。

### 2.1 泛型函数
- 类型参数
- 约束条件
- 默认类型

### 2.2 泛型类
- 类泛型
- 接口泛型
- 类型推断

## 3. 类型守卫
类型守卫帮助我们缩小类型范围。

### 3.1 自定义守卫
- 类型谓词
- 类型断言
- 类型保护

### 3.2 内置守卫
- instanceof
- typeof
- in 操作符

## 4. 最佳实践
TypeScript 开发中的一些最佳实践。

### 4.1 类型定义
- 接口设计
- 类型复用
- 类型导出

### 4.2 错误处理
- 类型错误
- 运行时错误
- 错误边界
    `,
    author: mockUsers[1],
    cover: "https://picsum.photos/400/200?random=2",
    summary: "TypeScript 高级类型实战",
    publishTime: "2025-01-12T11:00:00Z",
    createTime: "2025-03-15T11:00:00Z",
    updateTime: "2025-03-15T11:00:00Z",
    tags: ["TypeScript", "前端", "JavaScript"],
    likes: 85,
    comments: 10,
    views: 800,
    stars: 10,
    category:'other',
    rank:0
  },
  {
    id: "3",
    title: "这个文章标题超级长，主要用来对标题超出宽度进行隐藏测试，鼠标悬停时显示完整标题",
    content: `
    # Vue 3 新特性详解
    `,
    author: mockUsers[0],
    cover: "https://picsum.photos/400/200?random=3",
    summary: "这是摘要区域，需要对摘要也进行一个隐藏测试，同文章标题功能类似，不过摘要部分一般必标题要长，所以会设置区域稍微大一点，内容也要长一点",
    publishTime: "2024-03-12T11:00:00Z",
    createTime: "2024-03-15T11:00:00Z",
    updateTime: "2024-03-15T11:00:00Z",
    tags: ["Vue", "前端", "JavaScript"],
    likes: 85,
    comments: 10,
    views: 800,
    stars: 15,
    category:'teach',
    rank:800,
    isTop:true,
  },
  {
    id: "4",
    title: "Markdown 格式示例文章",
    cover: "https://picsum.photos/400/200.webp?random=4",
    summary: "Markdown 格式示例文章",
    author: mockUsers[0],
    publishTime: "2024-03-15T11:00:00Z",
    createTime: "2024-03-15T11:00:00Z",
    updateTime: "2024-03-15T11:00:00Z",
    views: 1234,
    likes: 88,
    stars: 10,
    comments: 32,
    tags: ["Markdown", "教程", "前端"],
    category:'teach',
    rank:800,
    content: `
# Markdown 格式示例

## 1. 基础格式

### 1.1 文本强调
*斜体文本* 和 _斜体文本_
**粗体文本** 和 __粗体文本__
***粗斜体文本*** 和 ___粗斜体文本___

### 1.2 列表
无序列表：
- 项目 1
- 项目 2
  - 子项目 2.1
  - 子项目 2.2

有序列表：
1. 第一步
2. 第二步
   1. 子步骤 2.1
   2. 子步骤 2.2

### 1.3 引用
> 这是一段引用文本
> 
> 这是引用的第二段
>> 这是嵌套引用

### 1.4 代码
行内代码：\`const hello = 'world'\`

代码块：
\`\`\`javascript
function hello() {
  console.log('Hello, world!');
}
\`\`\`

## 2. 高级格式

### 2.1 表格
| 表头 1 | 表头 2 | 表头 3 |
|--------|--------|--------|
| 单元格 1 | 单元格 2 | 单元格 3 |
| 单元格 4 | 单元格 5 | 单元格 6 |

### 2.2 链接
[链接文本](https://example.com)

### 2.3 任务列表
- [x] 已完成任务
- [ ] 未完成任务
- [ ] 另一个未完成任务

### 2.4 水平线
---
***
___
 

## 4 标签
\`标签1\` \`标签2\` \`标签3\`


### 5 代码高亮
\`\`\`typescript
interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "John",
  age: 30
};
\`\`\`

###  表格对齐
| 左对齐 | 居中对齐 | 右对齐 |
|:-------|:--------:|-------:|
| 内容   |   内容   |   内容 |

### 表格合并
| 合并单元格 | 列 2 | 列 3 |
|:----------|:-----|:-----|
| 内容      | 内容 | 内容 |
| 内容      | 内容 | 内容 |

    `,
  },
  {
    id: "5",
    title: "Node.js 性能优化实战指南",
    content: `
# Node.js 性能优化实战指南

## 1. 内存管理
Node.js 的内存管理是性能优化的关键。

### 1.1 垃圾回收
- V8 引擎的垃圾回收机制
- 内存泄漏的检测和修复
- 内存使用监控

### 1.2 内存优化
- 对象池模式
- 流式处理大数据
- 缓存策略优化

## 2. 异步编程优化
异步编程是 Node.js 的核心特性。

### 2.1 Promise 优化
- Promise 链的优化
- 错误处理的最佳实践
- 并发控制

### 2.2 Async/Await 优化
- 异步函数的性能考虑
- 错误边界处理
- 资源清理

## 3. 数据库优化
数据库操作是性能瓶颈的主要来源。

### 3.1 查询优化
- 索引优化
- 查询语句优化
- 连接池管理

### 3.2 缓存策略
- Redis 缓存
- 内存缓存
- 缓存失效策略

## 4. 网络优化
网络性能直接影响用户体验。

### 4.1 HTTP 优化
- 压缩和缓存
- 连接复用
- 负载均衡

### 4.2 WebSocket 优化
- 连接管理
- 消息队列
- 心跳机制
    `,
    author: mockUsers[2],
    cover: "https://picsum.photos/400/200?random=5",
    summary: "深入探讨 Node.js 性能优化的各个方面，包括内存管理、异步编程、数据库优化和网络优化等",
    publishTime: "2024-02-20T10:00:00Z",
    createTime: "2024-02-18T14:00:00Z",
    updateTime: "2024-02-20T10:00:00Z",
    tags: ["Node.js", "后端", "性能优化"],
    likes: 156,
    comments: 23,
    views: 2100,
    stars: 28,
    category: 'teach',
    rank: 950,
    difficulty: 'hard',
    isHot: true,
    isTop: true
  },
  {
    id: "6",
    title: "CSS Grid 布局完全指南",
    content: `
# CSS Grid 布局完全指南

## 1. Grid 基础
CSS Grid 是一种二维布局系统。

### 1.1 容器属性
- display: grid
- grid-template-columns
- grid-template-rows

### 1.2 项目属性
- grid-column
- grid-row
- grid-area

## 2. 高级特性
Grid 的高级布局特性。

### 2.1 自动布局
- grid-auto-flow
- grid-auto-columns
- grid-auto-rows

### 2.2 对齐方式
- justify-items
- align-items
- place-items

## 3. 响应式设计
Grid 在响应式设计中的应用。

### 3.1 媒体查询
- 断点设计
- 布局适配
- 内容重排

### 3.2 自适应网格
- minmax()
- auto-fit
- auto-fill

## 4. 实战案例
Grid 布局的实际应用。

### 4.1 卡片布局
- 等宽卡片
- 瀑布流布局
- 动态高度

### 4.2 页面布局
- 侧边栏布局
- 头部固定
- 内容区域
    `,
    author: mockUsers[2],
    cover: "",
    summary: "全面介绍 CSS Grid 布局的使用方法，从基础概念到高级应用，包含大量实战案例",
    publishTime: "2024-02-10T11:00:00Z",
    createTime: "2024-02-08T15:00:00Z",
    updateTime: "2024-02-10T11:00:00Z",
    tags: ["CSS", "前端", "布局"],
    likes: 142,
    comments: 19,
    views: 1800,
    stars: 22,
    category: 'teach',
    rank: 850,
    difficulty: 'medium'
  },
];
// 模拟标签数据
export const mockTags: Tags[] = [
  { id: "1", name: "React", count: 15 },
  { id: "2", name: "TypeScript", count: 12 },
  { id: "3", name: "JavaScript", count: 20 },
  { id: "4", name: "前端", count: 25 },
  { id: "5", name: "Node.js", count: 10 },
  { id: "6", name: "Vue", count: 14 },
  { id: "7", name: "CSS", count: 16 },
  { id: "8", name: "HTML", count: 8 },
  { id: "9", name: "性能优化", count: 11 },
];
// 模拟评论数据
export const mockComments: Comment[] = [
  {
    id: "1",
    content: "这篇文章写得很好，对React 18的新特性讲解得很清楚！",
    author: mockUsers[1],
    createTime: "2024-03-15T11:00:00Z",
    likes: 5,
    articleId: "2",
    replies: [
      {
        id: "3",
        content: "谢谢你的肯定！我会继续分享更多优质内容。",
        author: mockUsers[0],
        createTime: "2024-03-15T11:30:00Z",
        likes: 2,
        articleId: "2",
        parentId: "1",
        replyTo: mockUsers[1],
      },
      {
        id: "2",
        content: "谢谢你的肯定！我会继续分享更多优质内容。",
        author: mockUsers[2],
        createTime: "2024-03-16T11:30:00Z",
        likes: 2,
        articleId: "2",
        parentId: "3",
        replyTo: mockUsers[0],
      },
    ],
  },
  {
    id: "4",
    content: "TypeScript的高级类型确实很强大，感谢分享！",
    author: mockUsers[0],
    createTime: "2024-03-14T16:00:00Z",
    likes: 3,
    articleId: "1",
    replies: [
      {
        id: "5",
        content: "谢谢你的肯定！我会继续分享更多优质内容。",
        author: mockUsers[1],
        createTime: "2024-03-15T11:30:00Z",
        likes: 2,
        articleId: "1",
        parentId: "4",
        replyTo: mockUsers[0],
      },
      {
        id: "6",
        content: "我也是这么认为的，TypeScript 的类型系统确实很强大",
        author: mockUsers[2],
        createTime: "2024-03-16T11:30:00Z",
        likes: 2,
        articleId: "1",
        parentId: "4",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "7",
    content: "Node.js 性能优化这篇文章太实用了，特别是内存管理部分！",
    author: mockUsers[1],
    createTime: "2024-02-21T09:00:00Z",
    likes: 8,
    articleId: "5",
    replies: [
      {
        id: "8",
        content: "内存泄漏检测确实很重要，有什么推荐的监控工具吗？",
        author: mockUsers[2],
        createTime: "2024-02-21T10:30:00Z",
        likes: 3,
        articleId: "5",
        parentId: "7",
        replyTo: mockUsers[1],
      },
      {
        id: "9",
        content: "推荐使用 Node.js 内置的 --inspect 和 Chrome DevTools，或者使用 clinic.js",
        author: mockUsers[0],
        createTime: "2024-02-21T11:00:00Z",
        likes: 5,
        articleId: "5",
        parentId: "7",
        replyTo: mockUsers[2],
      },
    ],
  },
  {
    id: "10",
    content: "Vue 3 Composition API 的响应式系统讲解得很透彻！",
    author: mockUsers[2],
    createTime: "2024-01-16T14:00:00Z",
    likes: 12,
    articleId: "6",
    replies: [
      {
        id: "11",
        content: "Proxy 相比 Object.defineProperty 确实有很多优势",
        author: mockUsers[1],
        createTime: "2024-01-16T15:00:00Z",
        likes: 4,
        articleId: "6",
        parentId: "10",
        replyTo: mockUsers[2],
      },
    ],
  },
  {
    id: "12",
    content: "微前端架构这篇文章对团队协作很有帮助！",
    author: mockUsers[0],
    createTime: "2024-03-02T16:00:00Z",
    likes: 6,
    articleId: "7",
    replies: [
      {
        id: "13",
        content: "我们团队也在考虑微前端，有什么实践经验可以分享吗？",
        author: mockUsers[1],
        createTime: "2024-03-02T17:00:00Z",
        likes: 2,
        articleId: "7",
        parentId: "12",
        replyTo: mockUsers[0],
      },
      {
        id: "14",
        content: "建议先从简单的基座模式开始，逐步过渡到更复杂的方案",
        author: mockUsers[2],
        createTime: "2024-03-02T18:00:00Z",
        likes: 3,
        articleId: "7",
        parentId: "12",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "15",
    content: "CSS Grid 布局指南很详细，解决了我的布局难题！",
    author: mockUsers[1],
    createTime: "2024-02-11T13:00:00Z",
    likes: 7,
    articleId: "8",
    replies: [
      {
        id: "16",
        content: "Grid 和 Flexbox 结合使用效果更好",
        author: mockUsers[0],
        createTime: "2024-02-11T14:00:00Z",
        likes: 3,
        articleId: "8",
        parentId: "15",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "17",
    content: "Docker 容器化部署的最佳实践总结得很全面！",
    author: mockUsers[2],
    createTime: "2024-01-26T10:00:00Z",
    likes: 9,
    articleId: "9",
    replies: [
      {
        id: "18",
        content: "多阶段构建确实能大大减少镜像大小",
        author: mockUsers[1],
        createTime: "2024-01-26T11:00:00Z",
        likes: 4,
        articleId: "9",
        parentId: "17",
        replyTo: mockUsers[2],
      },
      {
        id: "19",
        content: "安全扫描也很重要，推荐使用 Trivy",
        author: mockUsers[0],
        createTime: "2024-01-26T12:00:00Z",
        likes: 2,
        articleId: "9",
        parentId: "17",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "20",
    content: "Web 安全防护指南对新手很有帮助，感谢分享！",
    author: mockUsers[0],
    createTime: "2024-03-06T15:00:00Z",
    likes: 11,
    articleId: "10",
    replies: [
      {
        id: "21",
        content: "XSS 和 CSRF 的防护措施讲解得很清楚",
        author: mockUsers[1],
        createTime: "2024-03-06T16:00:00Z",
        likes: 3,
        articleId: "10",
        parentId: "20",
        replyTo: mockUsers[0],
      },
    ],
  },
  {
    id: "22",
    content: "GraphQL vs REST 的对比分析很客观，帮助我做出了技术选择！",
    author: mockUsers[1],
    createTime: "2024-02-29T12:00:00Z",
    likes: 6,
    articleId: "11",
    replies: [
      {
        id: "23",
        content: "我们项目选择了 GraphQL，开发体验确实不错",
        author: mockUsers[2],
        createTime: "2024-02-29T13:00:00Z",
        likes: 2,
        articleId: "11",
        parentId: "22",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "24",
    content: "前端工程化实践总结对团队建设很有价值！",
    author: mockUsers[2],
    createTime: "2024-03-09T09:00:00Z",
    likes: 14,
    articleId: "12",
    replies: [
      {
        id: "25",
        content: "CI/CD 流程的自动化确实能提高开发效率",
        author: mockUsers[0],
        createTime: "2024-03-09T10:00:00Z",
        likes: 5,
        articleId: "12",
        parentId: "24",
        replyTo: mockUsers[2],
      },
      {
        id: "26",
        content: "代码质量工具链的配置也很重要",
        author: mockUsers[1],
        createTime: "2024-03-09T11:00:00Z",
        likes: 3,
        articleId: "12",
        parentId: "24",
        replyTo: mockUsers[2],
      },
    ],
  },
  {
    id: "27",
    content: "这篇文章的 Markdown 格式示例很全面，学到了很多！",
    author: mockUsers[1],
    createTime: "2024-03-16T08:00:00Z",
    likes: 4,
    articleId: "4",
    replies: [
      {
        id: "28",
        content: "数学公式和图表的支持确实很强大",
        author: mockUsers[2],
        createTime: "2024-03-16T09:00:00Z",
        likes: 2,
        articleId: "4",
        parentId: "27",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "29",
    content: "Vue 3 的新特性确实很吸引人，特别是 Composition API！",
    author: mockUsers[0],
    createTime: "2024-03-13T14:00:00Z",
    likes: 7,
    articleId: "3",
    replies: [
      {
        id: "30",
        content: "从 Options API 迁移到 Composition API 有什么建议吗？",
        author: mockUsers[1],
        createTime: "2024-03-13T15:00:00Z",
        likes: 3,
        articleId: "3",
        parentId: "29",
        replyTo: mockUsers[0],
      },
      {
        id: "31",
        content: "建议渐进式迁移，可以先从新组件开始使用",
        author: mockUsers[2],
        createTime: "2024-03-13T16:00:00Z",
        likes: 4,
        articleId: "3",
        parentId: "29",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "32",
    content: "这个长标题测试很有意思，UI 处理得很好！",
    author: mockUsers[2],
    createTime: "2024-03-12T11:00:00Z",
    likes: 3,
    articleId: "3",
    replies: [
      {
        id: "33",
        content: "文本截断和悬停显示完整内容的交互很友好",
        author: mockUsers[0],
        createTime: "2024-03-12T12:00:00Z",
        likes: 2,
        articleId: "3",
        parentId: "32",
        replyTo: mockUsers[2],
      },
    ],
  },
  {
    id: "34",
    content: "React 18 的并发特性对性能提升很明显！",
    author: mockUsers[1],
    createTime: "2024-06-16T10:00:00Z",
    likes: 15,
    articleId: "1",
    replies: [
      {
        id: "35",
        content: "useTransition 和 useDeferredValue 的使用场景讲解得很清楚",
        author: mockUsers[2],
        createTime: "2024-06-16T11:00:00Z",
        likes: 6,
        articleId: "1",
        parentId: "34",
        replyTo: mockUsers[1],
      },
      {
        id: "36",
        content: "自动批处理确实简化了很多代码",
        author: mockUsers[0],
        createTime: "2024-06-16T12:00:00Z",
        likes: 4,
        articleId: "1",
        parentId: "34",
        replyTo: mockUsers[1],
      },
    ],
  },
  {
    id: "37",
    content: "TypeScript 的高级类型系统太强大了，类型安全真的很重要！",
    author: mockUsers[2],
    createTime: "2024-01-13T16:00:00Z",
    likes: 9,
    articleId: "2",
    replies: [
      {
        id: "38",
        content: "条件类型和映射类型的组合使用很灵活",
        author: mockUsers[0],
        createTime: "2024-01-13T17:00:00Z",
        likes: 3,
        articleId: "2",
        parentId: "37",
        replyTo: mockUsers[2],
      },
    ],
  },
  {
    id: "39",
    content: "这些技术文章的质量都很高，感谢作者的分享！",
    author: mockUsers[1],
    createTime: "2024-03-10T13:00:00Z",
    likes: 18,
    articleId: "12",
    replies: [
      {
        id: "40",
        content: "确实，每篇文章都很实用，期待更多内容！",
        author: mockUsers[2],
        createTime: "2024-03-10T14:00:00Z",
        likes: 7,
        articleId: "12",
        parentId: "39",
        replyTo: mockUsers[1],
      },
      {
        id: "41",
        content: "作者的技术功底很深，讲解得很透彻",
        author: mockUsers[0],
        createTime: "2024-03-10T15:00:00Z",
        likes: 5,
        articleId: "12",
        parentId: "39",
        replyTo: mockUsers[1],
      },
    ],
  }
];

// 根据ID获取用户
export const getUserById = async (id: string): Promise<User | undefined> => {
  const users = await getMockUsers();
  if (!users) return undefined;
  return users.find((user) => user.id === id);
};

// 添加评论
export const addComment = async (
  comment: Omit<Comment, "id" | "createTime" | "likes">,
): Promise<Comment> => {
  const newComment: Comment = {
    ...comment,
    id: Date.now().toString(),
    createTime: new Date().toISOString(),
    likes: 0,
    replies: [],
  };

  await addMockComment(newComment);
  return newComment;
};

// 添加回复
export const addReply = async (
  reply: Omit<Comment, "id" | "createTime" | "likes">,
): Promise<Comment> => {
  const newReply: Comment = {
    ...reply,
    id: Date.now().toString(),
    createTime: new Date().toISOString(),
    likes: 0,
    replies: [],
  };

  const comments = await getMockComments();
  if (comments && reply.parentId) {
    // 查找目标评论的最顶层父评论ID
    const findTopLevelParentId = (targetId: string): string | null => {
      // 先检查是否是主评论
      const mainComment = comments.find((c) => c.id === targetId);
      if (mainComment) {
        return mainComment.id;
      }
      
      // 查找子评论的父评论
      for (const comment of comments) {
        if (comment.replies) {
          const reply = comment.replies.find((r) => r.id === targetId);
          if (reply) {
            return comment.id;
          }
        }
      }
      return null;
    };

    const topLevelParentId = findTopLevelParentId(reply.parentId);
    
    if (topLevelParentId) {
      const updatedComments = comments.map((comment) => {
        if (comment.id === topLevelParentId) {
          return {
            ...comment,
            replies: [...(comment.replies || []), newReply],
          };
        }
        return comment;
      });
      await updateMockComments(updatedComments);
    }
  }

  return newReply;
};

// 点赞评论
export const addLike = async (commentId: string): Promise<Comment | null> => {
  const comments = await getMockComments();
  if (comments) {
    // 查找主评论
    const mainComment = comments.find((c) => c.id === commentId);
    if (mainComment) {
      const newLikes = mainComment.likes + 1;
      await updateCommentLikes(commentId, newLikes);
      return { ...mainComment, likes: newLikes };
    }
    
    // 查找子评论
    for (const comment of comments) {
      if (comment.replies) {
        const reply = comment.replies.find((r) => r.id === commentId);
        if (reply) {
          const newLikes = reply.likes + 1;
          await updateCommentLikes(commentId, newLikes);
          return { ...reply, likes: newLikes };
        }
      }
    }
  }
  return null;
};

// 获取标签
export const getTags = async (): Promise<Tags[]> => {
  const tags = await getMockAllTags();
  return tags || [];
};

